<template>
  <div>
    <div class="h60">
      <span class="exnum">{{ Num }}.{{ item.title }}</span>
      <span class="labels">
        <span v-if="item.isJt == '1'">机经</span>
        <span v-if="item.isJt !== '1'" style="background: #7cc3d6">练习题</span>
        <span
          style="background: #ff5e4e"
          v-if="item.labelStyle == 'wrong-color'"
          >错题</span
        >
        <span style="background: #13bfbf" v-if="item.isYc == '1'">本周预测</span>
        <span style="background: #94d0f7">{{
          item.titleNumber
        }}</span>
      </span>
      <Label
        v-if="item.labelStyle != undefined"
        :df="item.labelStyle"
        :id="item.id"
      ></Label>
    </div>
    <div class="h383" @click="imgScc" :class="{ active: isChoose }">
      <div class="img-box">
        <img :src="item.url" alt="" />
      </div>
    </div>
    <div class="time">
      <span class="readyTime" v-if="readyTime > 0"
        >请准备：00:{{ readyTime > 9 ? readyTime : "0" + readyTime }}</span
      >
      <span v-if="readyTime == 0">答题时间：{{ s_to_hs(answerTime) }}</span>
    </div>

    <div class="insert">
      <div
        :class="!isOpen ? 'setpink' : ''"
        class="microphone"
        @click="handleClick"
      >
        <i class="iconfont icon-maikefeng"></i>
      </div>
      <div v-if="isOpen > 0" class="countdown">点击开始录音</div>
      <div class="countdown" v-else>
        <span ref="zeroHidden">点击结束录音</span>
      </div>
    </div>
    <!-- 提示音 -->
    <audio ref="audio" :src="audioUrl"></audio>
    <!-- 录音 -->
    <div class="record">
      <el-form ref="recorderForm">
        <Aplayer v-if="url" :url="url"></Aplayer>
      </el-form>
    </div>
  </div>
</template>

<script>
import writingUtil from "@/utils/writingUtil";
import Aplayer from '../../../../practice/exercise/aplayer'
export default {
  props: [ 'item', 'Num'],
  mixins: [writingUtil],
  components: {
    Aplayer
  },
  data () {
    return {
      audioUrl: require('../../../../../assets/alert/dingd.mp3'), // 提示音
      countDown: 30,
      readyTime: 25,//准备时间
      answerTime: 0,//答题时间
      recordingCountDown: 40, // 答题时间倒计时
      value: false, // 控制参考答案是否显示
      totalTime: 0, // 录音总时长
      percent: 0, // 播放进度比
      type: 'pause',
      answer: "123",//答案
      subject: {},//题目详情
      eSubjectType: {},//题型信息
      practiceList: [],//答题记录
      keyword: '',//搜索关键字
      last: '',//上一题
      next: '',//下一题
      id: '',
      url:'',
      interval: {},//循环组件
      isOpen: false,//是否开启录音
      isChoose: false,
      tips: false, // 麦克风是否授权
      dialogVisible: false,
      isAnswerShow: false,
      iNum: '' // 题目索引号
    }
  },
  created () {
    // 进入页面即请求麦克风权限
    this.getPermission()
   
    this.coutDown()
  },
  // 清除答题时间的定时器
  beforeDestroy () {
    clearInterval(this.interval.answerTime)
    this.url = ''
  },
  // destroyed () {
  //   var that = this
  //   this.url = ''
  //   that.getRecorder();
  // },
  watch: {
    readyTime: {
      handler () {
        var that = this
        if (this.readyTime == 0) {
          // console.log(1);
          that.startRecorder()
          this.$refs.audio.play();
          this.answerTimeCount()
        }
      }
    }
  },
  methods: {
    // 点击麦克风
    handleClick () {
      // 获取了麦克风权限才允许点击麦克录音
      if (this.tips) {
        this.isOpen = !this.isOpen
        if (!this.isOpen) { // 开启录音
          this.readyTime = 0
          this.$refs.audio.play();//点击触发 点击音效
          clearInterval(this.interval.answerTime)
          if (this.answerTime !== 0) {
            this.answerTime = 0
            this.answerTimeCount()
            // this.startRecorder()
          }
          this.url = '' // 再次录音的时候，把播放器文件清空
        } else { // 关闭录音

          this.getRecorder()
          clearInterval(this.interval.answerTime)
          this.$emit('sendAnswer',this.url)
        }
      } else {
        this.$msg.info('请先打开麦克风授权')
      }
    },
    imgScc: function () {
      this.isChoose = !this.isChoose
    },
    // 准备答题倒计时
    coutDown () {
      var that = this
      this.interval.mkf = setInterval(() => {
        if (this.readyTime > 0) {
          this.readyTime--
        } else {
          this.isOpen = false
        }
      }, 1000);
    },
    // 答题时间计时
    answerTimeCount () {
      var that = this
      this.url = ''
      clearInterval(this.interval.answerTime)
      this.answerTime = 0
      this.interval.answerTime = setInterval(() => {
        // 答题时间上限
        if (this.answerTime < 40) {
          this.answerTime++
        } else {
          this.answerTime = 40
          this.isOpen = true
          this.getRecorder()
          clearInterval(this.interval.answerTime)
        }
      }, 1000);
    },
  },
}
</script>

<style lang="less" scoped>
.setpink {
  background-color: #ff5e4e !important;
}
.readyTime {
  color: #ff5e4e;
}
.insert {
  .microphone {
    width: 4.375rem;
    height: 4.375rem;
    line-height: 4.375rem;
    text-align: center;
    margin-left: 47%;
    border-radius: 50%;
    cursor: pointer;
    background-color: #eee;
    .iconfont {
      font-size: 2.5rem;
      color: #fff;
    }
  }
  .countdown {
    height: 2.8125rem;
    line-height: 2.8125rem;
    font-size: 1rem;
    color: #333;
    text-align: center;
  }
}
.record {
  width: 420px;
  margin: 0 auto;
  position: relative;
  .cover {
    position: absolute;
    width: 1.25rem;
    height: 1.25rem;
    top: 1.5625rem;
    right: 2.8125rem;
    background-color: #f2f2f2;
  }
}
.h383 {
  transform: scale(1); /*图片原始大小1倍*/
  transition: all ease 0.5s;
} /*图片放大所用时间*/

.h383.active {
  transform: scale(1.6); /*图片需要放大3倍*/
  position: absolute; /*是相对于前面的容器定位的，此处要放大的图片，不能使用position：relative；以及float，否则会导致z-index无效*/
  z-index: 100;
}
.h60 {
  height: 3.75rem;
  line-height: 3.75rem;
  padding-left: 1.25rem;
  box-sizing: border-box;
  background-color: #f7f7f7;
  display: flex;
  position: relative;
  margin-bottom: 1rem;
  .exnum {
    width: 45rem;
    font-size: 1rem;
    color: #666;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .labels {
    display: flex;
    justify-content: flex-end;
    width: 21.25rem;
    span {
      height: 1.875rem;
      line-height: 1.875rem;
      padding: 0 0.375rem;
      box-sizing: border-box;
      background-color: #ff9b4f;
      border-radius: 0.1875rem;
      margin: 0.9375rem 0.46875rem;
      color: #fff;
      box-sizing: border-box;
    }
  }
}

.h383 {
  margin: 3.125rem 0 1.5625rem;
  background: #fffafa;
  border: 1px dashed #ff5e4e;
  border-bottom: 1px dashed #ff5e4e !important;
  box-sizing: border-box;
  padding: 1.875rem;
  box-sizing: border-box;
  font-size: 1.125rem;
  color: #666;
  line-height: 2rem;
  .img-box {
    width: 38.1875rem;
    // height: 20.1875rem;
    margin: auto;
    img {
      width: 100%;
      height: 100%;
    }
  }
}

.time {
  font-size: 1rem;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #333333;
  position: relative;
  span {
    margin-right: 1.875rem;
  }
  .iconfont {
    font-size: 1.5rem;
    color: #ccc;
  }
  .end {
    position: absolute;
    right: -1.5rem;
    text-align: right;
  }
}

 .btns {
        height: 4.125rem;
        width: 20.875rem;
        margin: auto;
        line-height: 2.5rem;

        .el-button {
          color: #ff5e4e;
          font-size: 1.125rem;
          text-align: center;
          width: 5rem;
          border-radius: 0.3125rem;
          border: 0.0625rem solid #ff5e4e;
          margin: 0.625rem 2.5rem;
          background-color: #fffafa;
        }
      }
</style>